<?php
include(dirname( __FILE__ )."../../pznahar.php");

$log = new log( 'log.txt');
$nottifi = new Nottification();
$nottifi->get();

if($nottifi->stop || $nottifi->text == '') exit;

$sr = new SecureRequest(APP_ID);
$sr->format('JSON');

$sql = " SELECT GROUP_CONCAT(users.kontaktid) as uids, 
        ( SELECT COUNT(*) as count FROM  `".PREFIX_TABLE."users` ) count
        FROM ( SELECT `kontaktid` FROM  `".PREFIX_TABLE."users` as users LIMIT ".$nottifi->count.",".($nottifi->count + 99) ." ) users ";

#print_r($sql."\n");

$log->start($sql);

if(!$result = DBConnect::query($sql)){
    $this->_error(mysql_error());
    return false;
    }


$data = DBConnect::getOne( $result );
$uids = $data['uids'];
#print_r($uids."\n");
if( !$result = $sr->request(SecureRequest::SEND_NOTIFICATION, array('uids' => $uids, 'message' => $nottifi->text )) ){
    $log->start('SEND_NOTIFICATION'. $sr->error);
}
$log->start( date( "D M j G:i:s T Y" ).' SEND_NOTIFICATION : '. $nottifi->count." - ". $nottifi->count + 99 );

$nottifi->count = $nottifi->count + 99 ; 
if( empty($uids) ){
    $nottifi->stop();
    $nottifi->all(1);
}else{
    $nottifi->start();
    $nottifi->all(0);    
}

$nottifi->save();
$log->end();

?>